package com.demo.search.service.impl;

import com.demo.search.pojo.Product;
import com.demo.search.service.SearchService;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SearchServiceImpl implements SearchService {


    @Autowired
    private SolrClient solrClient;

    @Override
    public Map<String, Object> searchKw(String kw, String fq, Integer pageNum, Integer pageSize) throws IOException {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q", "*:*");
        solrQuery.set("start", 0);
        solrQuery.set("rows", 20);

        //======高亮设置===
        //开启高亮
        solrQuery.setHighlight(true);
        //高亮域
        solrQuery.addHighlightField("title");
        //前缀
        solrQuery.setHighlightSimplePre("<span style='color:red'>");
        //后缀
        solrQuery.setHighlightSimplePost("</span>");

        QueryResponse response = null;
        try {
            response = solrClient.query(solrQuery);
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        SolrDocumentList results = response.getResults();
        Map<String, Object> map = new HashMap<>();
        map.put("results", results);
        return map;
    }
}
